﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP
{
    using System.Windows.Forms;
    using System;
    using System.IO;

    static class Program
    {
        /// <summary>The file name, where the options are saved.</summary>
        public static string OptionsFile;
        /// <summary>The program exe, which starts Picturez.</summary>
        public static string ProgramExe;
        /// <summary>The program directory, where Picturez is located.</summary>
        public static string ProgramPath;

        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            OptionsFile = Environment.GetFolderPath(
                Environment.SpecialFolder.LocalApplicationData) +
                "\\SBIP\\options.dat";

            ////Creates only one time after update from version 
            //// 0.93 to 0.94 (VISTA compatible)
            //if (!System.IO.File.Exists(OptionsFile))
            //    Program.CreateOptionsFile(OptionsFile);

            ProgramExe =
                Directory.GetParent(Application.ExecutablePath) +
                "\\SBIP.exe";
            ProgramPath =
                Directory.GetParent(Application.ExecutablePath).ToString();

            //Creates only by very first start of SBIP
            if (!File.Exists(OptionsFile))
            {
                CreateOptionsFile(OptionsFile);
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
          
            ProcessorForm form;
            // start with parameter
            if (args.Length == 2 && args[1] == "sbip.frank")
            {
                form = new ProcessorForm(args[0]);
            }
            // normal start
            else
                form = new ProcessorForm();



            //Application.Run(form1);

            form.Show();
            while (form.Created)
            {
                form.Processor.Render();
                form.UpdateEachFrame(false);
                Application.DoEvents();
            }

            #region TODO Frank Nagl: Delete this region.
            //Bitmap image = new Bitmap("sample1.jpg");

            //// 1. Offline rendering
            //// create Processor, used as rendering framework
            //Processor processor = new Processor();
            //// starts Processor
            //processor.Begin(image);
            //// create RotateChannels filter
            //RotateChannels filter = new RotateChannels();
            //processor.Filter = filter;
            //// optional: configure filter
            //filter.Order = RGBOrder.GBR;
            //// apply the filter
            //Bitmap resultImage = processor.RenderToBitmap();
            ////Texture2D resultTexture = processor.RenderToTexture( );
            //resultImage.Save("RotateChannels.jpg", ImageFormat.Jpeg);
            //processor.End();

            //// 2. Online rendering
            //// create any windows control for rendering in
            //Form myForm = new Form();
            //// create Processor, used as rendering framework
            //Processor processor2 = new Processor();
            //// starts Processor
            //processor2.Begin(image, myForm);
            //// create ExtractChannel filter
            //RotateChannels filter2 = new RotateChannels();
            //processor2.Filter = filter2;
            //// optional: configure filter
            //filter2.Order = RGBOrder.GBR;

            //// apply the filter
            //myForm.Show();
            //while (myForm.Created)
            //{
            //    processor2.Render();
            //    Application.DoEvents();
            //}
            //processor2.End();

            #endregion TODO Frank Nagl: Delete this region.
        }

        /// <summary>
        /// Creates the options file after changing path from 'ExecutablePath' 
        /// to 'LocalApplicationData' (necessary for VISTA).
        /// </summary>
        /// <remarks>
        /// Creates only one time after update from version 1.31 to 1.32
        /// </remarks>
        /// <param name="file">The path to the options file.</param>
        private static void CreateOptionsFile(string file)
        {
            string dir = file.Substring(0, file.LastIndexOf('\\'));
            Directory.CreateDirectory(dir);
            FileStream f = File.Create(file);
            f.Close();
            WriteLine(file, 1, "Version;1.0", true);
            WriteLine(file, 2, "ReleaseDate;2010-08-06", true);
            //WriteLine(file, 3, "Fastcut;100,100,100,100", true);
            //WriteLine(file, 4, "LineStrength;2", true);
            //WriteLine(file, 5, "Language;English", true);
        }

        /// <summary>
        /// Source from nail.dll: Schreibt den übergebenen Text in eine 
        /// definierte Zeile.
        /// </summary>
        /// <param name="filename">Pfad zur Datei</param>
        /// <param name="line">Zeilennummer</param>
        /// <param name="lines">Text für die übergebene Zeile</param>
        /// <param name="replace">Text in dieser Zeile überschreiben (t) 
        /// oder einfügen (f)</param>
        private static void WriteLine(
            String filename, int line, string lines, bool replace)
        {
            string tContent = "";
            string[] tDelimiterstring = { "\r\n" };

            if (File.Exists(filename))
            {
                StreamReader tFile = new StreamReader(
                    filename, 
                    System.Text.Encoding.Default);
                tContent = tFile.ReadToEnd();
                tFile.Close();
            }

            string[] tCols = tContent.Split(
                tDelimiterstring, 
                StringSplitOptions.None);

            if (tCols.Length >= line)
            {
                if (!replace)
                    tCols[line - 1] = lines + "\r\n" + tCols[line - 1];
                else
                    tCols[line - 1] = lines;

                tContent = "";
                for (int x = 0; x < tCols.Length - 1; x++)
                {
                    tContent += tCols[x] + "\r\n";
                }
                tContent += tCols[tCols.Length - 1];

            }
            else
            {
                for (int x = 0; x < line - tCols.Length; x++)
                    tContent += "\r\n";

                tContent += lines;
            }


            StreamWriter tSaveFile = new StreamWriter(filename);
            tSaveFile.Write(tContent);
            tSaveFile.Close();
        }
    }
}
